package leeon.mobile.server.bbscache.entity;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import leeon.mobile.BBSBrowser.models.BoardObject;

@PersistenceCapable(detachable = "true")
public class Board {
	
	@PrimaryKey
	@Persistent
	private String name;

	@Persistent
	private String parent;
	
	@Persistent
    @Extension(vendorName="datanucleus", key="gae.unindexed", value="true")
	private Long id;
	
	@Persistent
    @Extension(vendorName="datanucleus", key="gae.unindexed", value="true")
	private String type;
	
	@Persistent
    @Extension(vendorName="datanucleus", key="gae.unindexed", value="true")
	private String ch;
	
	@Persistent
    @Extension(vendorName="datanucleus", key="gae.unindexed", value="true")
	private String master;
	
	@Persistent
    @Extension(vendorName="datanucleus", key="gae.unindexed", value="true")
	private int total = 0;
	
	@Persistent
    @Extension(vendorName="datanucleus", key="gae.unindexed", value="true")
	private int totalG = 0;
	
	@Persistent
    @Extension(vendorName="datanucleus", key="gae.unindexed", value="true")
	private boolean dir = false;
	
	@Persistent
    @Extension(vendorName="datanucleus", key="gae.unindexed", value="true")
	private boolean recom = false;
	
	public Board(String name, String parent, String type, String ch, String master, boolean dir, boolean recom) {
		this.name = name;
		this.parent = parent;
		this.type = type;
		this.ch = ch;
		this.master = master;
		this.dir = dir;
		this.recom = recom;
	}

	
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getParent() {
		return parent;
	}

	public void setParent(String parent) {
		this.parent = parent;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getCh() {
		return ch;
	}

	public void setCh(String ch) {
		this.ch = ch;
	}

	public String getMaster() {
		return master;
	}

	public void setMaster(String master) {
		this.master = master;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public int getTotalG() {
		return totalG;
	}

	public void setTotalG(int totalG) {
		this.totalG = totalG;
	}

	public boolean isDir() {
		return dir;
	}

	public void setDir(boolean dir) {
		this.dir = dir;
	}
	
	public boolean isRecom() {
		return recom;
	}

	public void setRecom(boolean recom) {
		this.recom = recom;
	}


	public BoardObject toBoardObject() {
		BoardObject b = new BoardObject(id!=null?String.valueOf(id):null, name, type, ch, master, String.valueOf(total), dir, total);
		b.setTotalG(totalG);
		return b;
	}
	
	public static Board toBoard(BoardObject b, String parent, boolean recom) {
		Board board = new Board(b.getName(), parent, b.getType(), b.getCh(), b.getMaster(), b.isDir(), recom);
		if (b.getId() != null && b.getId().length() != 0) {
			board.setId(Long.valueOf(b.getId()));
			board.setTotal(b.getTotal());
			board.setTotalG(b.getTotalG());
		}
		return board;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		return name.equals(((Board)obj).name);
	}
	
}
